package studio.banner.reference.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import studio.banner.reference.entity.UserLogin;
import studio.banner.reference.mapper.UserLoginMapper;
import studio.banner.reference.util.ShiroUtil;
@Service
public class  UserLoginService {
	@Autowired
	private UserLoginMapper userLoginMapper;
	
	private ShiroUtil shiroUtil=null;
	
	/*
	 * 查询userlogin表中的密码，返回password
	 */
	public String findUserLogin(String username) {
		UserLogin userlogin=new UserLogin();
		userlogin.setUsername(username);
//		根据单列查询
		QueryWrapper<UserLogin> wrapper=new QueryWrapper<>();
		wrapper.eq("username", username);
		  UserLogin result = userLoginMapper.selectOne(wrapper);
		  try {
			  if(result.getId()!=null) {
				  return result.getPassword();
			  }
//			  0表示为空
			 
		} catch (Exception e) {
			// TODO: handle exception
		}
		  return null;
	}
	/*
	 * 根据用户名更新password
	 */
	public Integer update(UserLogin userLogin) {
		shiroUtil=new ShiroUtil();
		Integer result=shiroUtil.resultUpdate(userLogin);
		if(result!=0) {
			userLogin.setPassword(ShiroUtil.getMd5(userLogin.getPassword(), userLogin.getUsername()));
			return userLoginMapper.updateById(userLogin);
		}
		return 0;
		
	}
	
}
